﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// 1.获取需求数量日期
    /// 2.展开BOM，损耗规则
    /// 3.找到流程，创建流程
    /// 4.创建订单关系
    /// 5.动态产生下层需求
    /// 6.下层需求自动合并
    /// </summary>
    public partial class UIAB05RZ: UIABPOS
    {
        #region ==IsDI===========
        public static readonly DependencyProperty IsDIProperty = DependencyProperty.Register("IsDI",
            typeof(bool), typeof(UIAB05RZ),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDIChanged));
        public bool IsDI
        {
            get
            {
                return (bool)GetValue(IsDIProperty);
            }
            set
            {
                SetValue(IsDIProperty, value);
            }
        }
        private static void OnIsDIChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAB05RZ;
            if (src != null)
            {
                src.IsDI = (bool)e.NewValue;
            }
        }
        #endregion

        public UIAB05RZ()
        { 
            MESSAGES += "RZABPNEW;";
            MESSAGES += "LOADABP;RESETPROD;"; 
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                IsCanChg = false;

            }
            else
            {
                IsCanChg = true;
            }
            if (dwRule != null)
            {
                VKDELDATE = dwRule["P7"].ToString();
            }
        }

        #region====WorkFlow========
        MessageInfo Lastmsg
        {
            get
            {
                return TMV.Lastmsg;
            }
            set
            {
                TMV.Lastmsg = value;
            }
        }

        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            IsBusy = true;
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {

                case "RZABPNEW":
                    if (Lastmsg != null)
                    {
                        //MessageBox.Show("请先完成前一个订单创建");
                        //return;
                    }
                    RULEKEY = "";//先装载规则
                    RULEKEY = msg.ORDER;
                    Lastmsg = msg;
                    ARTST = msg.WAKZ;
                    ART = msg.ART;
                    VAR = msg.VAR;
                    FARBE = msg.FARBE;
                    FINISH = msg.BESCHICH;
                    DESS = msg.DESS;
                    COLORIT = msg.COLORIT;
                    FSIZE = msg.GROE;
                    DESS = msg.DESS;
                    COLORIT = msg.COLORIT;
                    FSIZE = msg.GROE;
                    FINISH = msg.BESCHICH;
                    PRART = ARTST + ART + VAR + FARBE + FINISH + DESS + COLORIT;
                    PM["ORDER"] = msg.ORDER;
                    PM["WAKZ"] = msg.WAKZ;
                    PM["ART"] = msg.ART;
                    PM["VAR"] = msg.VAR;
                    PM["FARBE"] = msg.FARBE;
                    PM["BESCHICH"] = msg.BESCHICH.PadRight(10);
                    PM["DESS"] = msg.DESS.PadRight(10);
                    PM["COLORIT"] = msg.COLORIT.PadRight(10);
                    PM["GROE"] = msg.GROE.PadRight(7);
                    PM["MAKE2ORD"] = MAKE2ORD;
                    PM["IsDI"] = IsDI;
                    IsOK = false;
                    TMV.RZABPNEW(PM, ref IsOK);
                    IsABPCreated = IsOK;
                    if (IsABPCreated)
                    {
                        ORDERNUM = "";
                        ORDERNUM = msg.ORDER;
                    }
                    break;
            }

            IsBusy = false;
        }
        #endregion

        public override void OnAutoChg(bool auto)
        {
            if (!auto)
                return;
            if (dwRule == null)
            {
            }
            base.OnAutoChg(auto);
        }

        public void btnPrePlan_Click(object sender, RoutedEventArgs e)
        {
            if (dwAB == null)
                return;
            IsABPCreated = false;
            if (Lastmsg == null)
            {
                Lastmsg = new MessageInfo();
                Lastmsg.ORDER = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString();
                Lastmsg.WAKZ = dwAB["ABPWAKZ"].ToString();
                Lastmsg.ART = dwAB["ABPART"].ToString();
                Lastmsg.VAR = dwAB["ABPVAR"].ToString();
                Lastmsg.FARBE = dwAB["ABPFARBE"].ToString();
                Lastmsg.BESCHICH = dwAB["ABPBESCHICH"].ToString();
                Lastmsg.DESS = dwAB["ABPDESS"].ToString();
                Lastmsg.COLORIT = dwAB["ABPCOLORIT"].ToString();
                Lastmsg.GROE = dwAB["ABPGROE"].ToString();
            }
            PM["ORDER"] = Lastmsg.ORDER;
            PM["WAKZ"] = Lastmsg.WAKZ;
            PM["ART"] = Lastmsg.ART;
            PM["VAR"] = Lastmsg.VAR;
            PM["FARBE"] = Lastmsg.FARBE.PadRight(10);
            PM["BESCHICH"] = Lastmsg.BESCHICH.PadRight(10);
            PM["DESS"] = Lastmsg.DESS.PadRight(10);
            PM["COLORIT"] = Lastmsg.COLORIT.PadRight(10);
            PM["GROE"] = Lastmsg.GROE.PadRight(7);
            PM["MAKE2ORD"] = MAKE2ORD;
            PM["IsDI"] = IsDI;
            IsOK = false;
            TMV.RZABPNEW(PM, ref IsOK);
            IsABPCreated = IsOK;
            if (IsABPCreated && dwAB != null)
            {
                string key = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                ORDERNUM = "";
                ORDERNUM = key;
            }
            IsShowAB = false;
            IsShowAB = true;
        }
        public override void OnEXAUFChg(string exauf)
        {
            IsReset = !IsReset;
            IsReset = !IsReset;
            if (!IsCanLoad)
                return;
            if (exauf.Length < 11)
                return;
            /*if (!exauf.StartsWith("V"))
                return;*/
            ORDERNUM = "";
            KRS = exauf.Substring(0, 2);
            LFD = exauf.Substring(2, 6);
            POS = exauf.Substring(8, 3);
            ORDERNUM = KRS + LFD + POS + POSTYP;
            base.OnEXAUFChg(exauf);
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            base.LOADBYAUF();
        }
        public override void OnFLOWChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (FLOW == "")
                return;
            if (dwRule == null)
                return;
            TMV.ABPANRKR = FLOW.Substring(0, 2);
            TMV.ABPALFDNR = FLOW.Substring(2, 6);
            TMV.UABP19(dwMaster);
            PM["ORDERNUM"] = TMV.ABPAUFKRS + TMV.ABPAUFLFD + TMV.ABPAUFPOS + TMV.ABPPOSTYP + "V";
            PM["AUFTYP"] = "AB05";
            TMV.VABF.ABFNEW(PM);
            TMV.VPR.UpdateICC_PRULES(dwRule, 1);
            string key = KEY;
            KEY = "";
            KEY = key;
            base.OnFLOWChg(value);
        }
       
        public override void OnWSChg(DataRow _dwWS)
        {
            if (_dwWS == null)
               TMV.WALAEAEND = 0;
            else
            {
                TMV.WALAEAEND = System.Convert.ToDouble(_dwWS["WALAEAEND"].ToString());
            }
            base.OnWSChg(_dwWS);
        }
    }
    partial class VABPOS
    {   
        public string INMODE = "";
        public string OUTMODE = "";
        public DataRow RZABPNEW(Dictionary<string, object> pm, ref bool IsABPCreated)
        {

            DataRow dwABP = null;
            try
            {
                double WARSTDLG = 10000;
              
                string UM = "MTR";
                double WAFBRTL = 1200;
                double WARGRLFD = 200;
                double WAFGRLFD = 200;
                double WAFSTDLG = 10000;
                double WARWBRT = 1600;
                bool IsColorEntry = true;
                if (dwWS != null)
                {
                    WARSTDLG = Convert.ToDouble(dwWS["WARSTDLG"].ToString());
                    WALAEAEND = System.Convert.ToDouble(dwWS["WALAEAEND"].ToString());
                    UM = dwWS["WAUEBM"].ToString();
                    WAFBRTL = System.Convert.ToDouble(dwWS["WAFBRTL"].ToString());
                    WARGRLFD = System.Convert.ToDouble(dwWS["WARGRLFD"].ToString());
                    WAFGRLFD = System.Convert.ToDouble(dwWS["WAFGRLFD"].ToString());
                    WAFSTDLG = System.Convert.ToDouble(dwWS["WAFSTDLG"].ToString());
                    WARWBRT = System.Convert.ToDouble(dwWS["WARWBRT"].ToString());
                    R01 = WALAEAEND;
                }
                if (dwRule == null)
                {
                    POSTYP = "VA ";
                    PRLO = "01";
                    PRLB = "HZ";
                    APZANRKRS = pm["UNDES"].ToString().Substring(0, 2);
                    APZALFDNR = pm["UNDES"].ToString().Substring(2, 6);
                    PM["VKAUF"] = "".PadRight(11);
                    PM["VKPAUF"] = pm["VKPAUF"].ToString();
                    PM["VKDELDATE"] = date;
                    INMODE = "F";
                    OUTMODE = "FBD";
                }
                else
                {
                    if (dwRule["UNDES"].ToString().Trim().Length < 8)
                        throw new Exception("请选择流程"); ;
                    if (PRLO.Length < 2)
                        throw new Exception("请选择流程");
                    if (PRLB.Length < 2)
                        throw new Exception("请选择流程"); ;
                    APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
                    APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
                    PM["VKAUF"] = dwRule["P4"];
                    PM["VKPAUF"] = dwRule["P5"];
                    PM["VKDELDATE"] = dwRule["P7"];
                    INMODE = dwRule["P8"].ToString();
                    OUTMODE = dwRule["P9"].ToString();
                }

                PM["KRS"] = KRS;

                if (APZALFDNR.Trim().Length < 6)
                {
                    throw new Exception("请选择流程");
                }
                #region==============规则================ 
                QTYMAT = 10000;
                double _R01 = 0;
                double _R02 = 0;
                double _F01 = 0;
                double _F02 = 0;
                double _LEADTIME = 0;
                if (dwRule != null)
                {
                    _R01 = R01 / 10000;//整理缩率
                    _R02 = R02 / 100;//零次布
                    _F02 = F02;//成品可用  
                    //JNUM//匹数
                    //JWASTAGE//接缝损耗
                   //P12头尾损耗，
                   //P07 根据订单量的损耗
                    _LEADTIME = Convert.ToDouble(LEADTIME);//生产周期
                }

                if (F02 > 0)
                {
                    QTYEND = QTYEND - F02;
                }

                string STARTD = Helpers.Helper.GetDateAddDays(DELDATE, Convert.ToInt16(0 - _LEADTIME));
                if (STARTD.Trim() == "") STARTD = "0";


                if (WAFSTDLG == 0)
                {
                    WAFSTDLG = 100000;
                }
                if (P12.Trim() == "") P12 = "0";
                QTYMAT = Math.Ceiling((QTYEND 
                    + double.Parse(P12) * 0.01 
                    + (double.Parse(JNUM) - 1) * double.Parse(JWASTGE))
                     * (1 + R01)
                      * (1 + 0.01*double.Parse(P11))
                    * (1 + R02 ) );
                #endregion
                #region=======dwData==========
                dwABP = TBMain.NewRow();
                SetUpNewRow(ref dwABP, null);
                ShareedABP(dwABP, pm);
                dwABP["ABPAENDZL"] = 6;
                dwABP["ABPMANDANT"] = app.Mandant;
                dwABP["ABPWAKZ"] = "F";
                dwABP["ABPAUFART"] = "VA";
                dwABP["ABPSERIE"] = " ";
                dwABP["ABPSME"] = 2;
                dwABP["ABPWAART"] = "   ";
                dwABP["ABPERSTBE"] = "E";
                dwABP["ABPSZUSATZKZ1"] = "F";
                dwABP["ABPMGV"] = "10";
                dwABP["ABPKDDAT"] = DELDATE;
                dwABP["ABPARTEX"] = dwRule["P12"].ToString().PadRight(10);
                dwABP["ABPSTDLAE"] = WARSTDLG / 10;
                dwABP["ABPMGV"] = 10;
                dwABP["ABPMGB"] = 9999999;
                dwABP["ABPZLO"] = PRLO;
                dwABP["ABPZLB"] = PRLB;
                dwABP["ABPLAEAENDA"] = WALAEAEND;
                dwABP["ABPSOLLMENGE1"] = QTYMAT * 100;
                dwABP["ABPSOLLMENGE2"] = QTYEND * 100;
                dwABP["ABPSOLLMENGE3"] = 100 * Math.Ceiling(QTYMAT * WARWBRT * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE4"] = 100 * Math.Ceiling(QTYEND * WAFBRTL * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE5"] = 100 * Math.Ceiling(QTYMAT * WARGRLFD * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE6"] = 100 * Math.Ceiling(QTYEND * WAFGRLFD * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE7"] = 100 * Math.Ceiling(QTYEND / (WAFSTDLG * Convert.ToDouble(0.001)));
                dwABP["ABPSOLLMENGE8"] = 100 * Math.Ceiling(QTYMAT / (WARSTDLG * Convert.ToDouble(0.001)));
                string vkp = dwRule["P5"].ToString().PadRight(11).Substring(0, 11);
                dwABP["ABPKDPARTIE"] = vkp.Substring(0, 2) + vkp.Substring(4, 4) + "-" + vkp.Substring(8, 3);//销售单号
                #endregion

                Insert(dwABP);
                IsABPCreated = true;
                if (dwRule != null)
                {
                    VPR.UpdateICC_PRULES(dwRule, 1);
                }
                //SELECT * FROM WDECK WHERE WDAUFKRS||WDAUFLFD||WDAUFPOS='F5000035004' OR WDAUFKRS||WDAUFLFD||WDAUFPOS='HZ000102001'
                PreparePM(dwABP);//
                PM["WAKZ"] = "F";
                PM["WDTYP"] = "Z";
                PM["INMODE"] = INMODE;
                PM["OUTMODE"] = OUTMODE;
                VWD.NEWWDZ(PM);
                VPAR.NABPR(PM);
                VABF.ABFNEW(PM);
                PM["ABAAUFTYP"] = "V";
                DataTable tbMU = VPR.LOAD(PM).Tables[0];
                foreach (DataRow row in tbMU.Rows)
                {
                    PM["VKAUF"] = row["P4"].ToString();
                    PM["ANZSTK"] = row["VKPQTY"].ToString();
                    PM["DELDATE"] = row["DELDATE"].ToString();
                    PM["QTYEND"] = row["VKPQTY"].ToString();
                    PM["VKPQTY"] = row["VKPQTY"].ToString();
                    VMU.MUNEW(PM);
                }

                PM["WAKZ"] = "R";
                PM["WDTYP"] = "B";
                PM["MAKE2ORD"] = pm["MAKE2ORD"];
                PM["IsDI"] = pm["IsDI"];
                VWD.NEWWDB(PM);
                Lastmsg = null;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {

            }
            return dwABP;
        }
    }
}
